<?php

namespace App\Http\Controllers;

use App\Http\Requests\CreateAdminInput;
use App\Http\Requests\UpdateAdminNickname;
use App\Http\Requests\UpdateAdminPassword;
use App\Models\Admin;
use App\Models\AdminAccount;
use App\Models\AdminToken;
use Illuminate\Support\Str;
use Yo;
use Login;

class AdminController extends Controller
{
    public function change_nickname(UpdateAdminNickname $request)
    {
        Login::admin(7);
        $nickname = $request->post('nickname');
        Admin::where('id', Login::$info->id)->update([
            'nickname' => $nickname
        ]);
        return Yo::update_echo(Login::$info->id);
    }

    public function change_password(UpdateAdminPassword $request)
    {
        Login::admin(7);
        $id = request()->post('id');
        $password = $request->post('password');
        $old_password = request()->post('old_password');
        $admin_account = AdminAccount::where('id', $id)->where('admin_id', Login::$info->id)->first();
        if (!$admin_account) Yo::error_echo(100008);
        if (!password_verify($old_password, $admin_account->secret)) Yo::error_echo(100017);
        if ($old_password === $password) Yo::error_echo(100018);
        $admin_account->secret = bcrypt($password);
        $admin_account->save();
        return Yo::update_echo(Login::$info->id);
    }

    public function admin_info()
    {
        Login::admin(6);
        $id = request()->post('id');
        $admin = Admin::select('id', 'nickname', 'admin_auth_id', 'status')->find($id);
        $admin_account = AdminAccount::select('id', 'account')->where('admin_id', $admin->id)->where('type', 1)->first();
        return Yo::echo([
            'info' => $admin,
            'account' => $admin_account,
        ]);
    }

    public function list()
    {
        Login::admin(6);
        $status = request()->post('status');
        $search = request()->post('search');
        $admin_list = Admin::select('id', 'nickname', 'status')
            ->selectRaw("IFNULL((select account from admin_accounts where admins.id = admin_accounts.admin_id and del = 2),'') as account")
            ->where(function ($query) use ($status) {
                if ($status != 0) $query->where('status', $status);
            })
            ->where('del', 2)
            ->where(function ($query) use ($search) {
                if ($search != '') $query->where('nickname', 'like', "%$search%");
            })->paginate(15);
        return Yo::echo($admin_list);
    }

    public function delete()
    {
        Login::admin(6);
        $ids = request()->post('ids');
        Admin::whereIn('id', $ids)->update([
            'del' => 1
        ]);
        AdminAccount::whereIn('admin_id', $ids)->where('del', 2)->update([
            'del' => 1
        ]);
        return Yo::delete_echo($ids);
    }

    public function update_status()
    {
        Login::admin(6);
        $id = request()->post('id');
        if ($id == 1) Yo::error_echo(100016);
        $status = request()->post('status');
        if ($status !== 2) $status = 1;
        Admin::where('id', $id)->update([
            'status' => $status
        ]);
        return Yo::update_echo($id);
    }

    public function update_auth()
    {
        Login::admin(6);
        $id = request()->post('id');
        if ($id == 1) Yo::error_echo(100016);
        $admin_auth_id = request()->post('admin_auth_id');
        Admin::where('id', $id)->update([
            'admin_auth_id' => $admin_auth_id
        ]);
        return Yo::update_echo($id);
    }

    public function update_nickname(UpdateAdminNickname $request)
    {
        Login::admin(6);
        $id = request()->post('id');
        $nickname = $request->post('nickname');
        Admin::where('id', $id)->update([
            'nickname' => $nickname
        ]);
        return Yo::update_echo($id);
    }

    public function update_password(UpdateAdminPassword $request)
    {
        Login::admin(6);
        $id = request()->post('id');
        $password = $request->post('password');
        AdminAccount::where('id', $id)->update([
            'secret' => bcrypt($password)
        ]);
        return Yo::update_echo($id);
    }

    public function create(CreateAdminInput $request)
    {
        Login::admin(6);
        $nickname = $request->post('nickname');
        $account = $request->post('account');
        $password = $request->post('password');
        $admin_auth_id = request()->post('admin_auth_id');
        if ($admin_auth_id == -1) Yo::error_echo(100016);
        $admin_account_check = AdminAccount::select('id')
            ->where('account', $account)
            ->where('type', 1)
            ->where('del', 2)
            ->first();
        if ($admin_account_check) Yo::error_echo(100015);
        $admin = new Admin();
        $admin->nickname = $nickname;
        $admin->admin_auth_id = $admin_auth_id;
        $admin->save();
        $admin_account = new AdminAccount();
        $admin_account->admin_id = $admin->id;
        $admin_account->account = $account;
        $admin_account->secret = bcrypt($password);
        $admin_account->type = 1;
        $admin_account->save();
        return Yo::create_echo($admin->id);
    }

    public function info()
    {
        Login::admin(3);
        $admin_account = AdminAccount::where('admin_id', Login::$info->id)
            ->where('type', 1)
            ->where('del', 2)
            ->first();
        return Yo::echo([
            'info' => [
                'id' => Login::$info->id,
                'account' => $admin_account ? $admin_account->account : '',
                'account_id' => $admin_account ? $admin_account->id : 0,
                'nickname' => Login::$info->nickname,
            ]
        ]);
    }

    public function status()
    {
        Login::admin(3);
        return Yo::echo();
    }

    public function create_token($admin, $type)
    {
        if ($admin->status != 1) Yo::error_echo(100002);
        if ($admin->del != 2) Yo::error_echo(100002);
        $token = Str::orderedUuid();
        AdminToken::create([
            'admin_id' => $admin->id,
            'token' => $token,
            'type' => $type,
            'del' => 2
        ]);
        return $token;
    }

    public function admin_login()
    {
        $account = request()->post('account');
        $password = request()->post('password');
        $type = 1;
        $admin_account = AdminAccount::where('account', $account)
            ->where('type', 1)
            ->where('del', 2)
            ->first();
        if (!$admin_account) Yo::error_echo(100001);
        if (!password_verify($password, $admin_account->secret)) Yo::error_echo(100002);
        $admin = Admin::where('id', $admin_account->admin_id)
            ->where('status', 1)
            ->where('del', 2)
            ->first();
        if (!$admin) Yo::error_echo(100001);
        Login::$info = $admin;
        Login::$login_type = $type;
        $auth_check_res = Login::check_admin_auth(2);
        if ($auth_check_res !== 0) Yo::error_echo($auth_check_res);
        $token = $this->create_token($admin, $type);
        return Yo::echo([
            'token' => $token
        ]);
    }
}
